﻿using System;
using System.Diagnostics;

namespace Project3_LargestPrimeFactor {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine("Project Euler Problem 3: What is the largest prime factor of the number 600851475143?");
            Stopwatch timer = new Stopwatch();

            const long LIMIT = 600851475143;

            timer.Start();

            long largest = 0;

            for (long count = 2; count < Math.Sqrt(LIMIT); count++) {
                bool isPrime = true;
                for (long divisor = 2; divisor < (long)Math.Ceiling(Math.Sqrt(count)); divisor++) {
                    if (count % divisor == 0) {
                        isPrime = false;
                        break;
                    }
                }

                if (isPrime && LIMIT % count == 0) {
                    largest = count;
                }
            }

            timer.Stop();
            Console.WriteLine("\nAnswer: {0}\nin {1}ms ({2} ticks)", largest, timer.ElapsedMilliseconds, timer.ElapsedTicks);
            Console.WriteLine("\nPress RETURN to exit...");
            Console.ReadLine();
        }
    }
}
